package com.dingpeikeji.quickdeer.modules.officeup.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.modules.base.web.FSysController;
import com.dingpeikeji.quickdeer.modules.officeup.entity.TOfficeUp;
import com.dingpeikeji.quickdeer.modules.officeup.service.TOfficeUpService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;
import com.google.common.collect.Lists;

/**
 * 普通客户升级
 */
@Controller
@RequestMapping(value = "/f/officeup/tOfficeUp")
public class FTOfficeUpController extends BaseController {

	@Autowired
	private TOfficeUpService tOfficeUpService;
	
	@ModelAttribute
	public void filter(Model model, HttpServletRequest request) {
		
		// 类似过滤器，如果是用户是企业类型，则可使用该类下的方法，否则提示他不可操作
		
		User user = (User) request.getSession().getAttribute(FSysController.FRONT_USER_IN_SESSION);
		boolean pass = true; // 是否可以使用该功能
		if(user == null) {
			pass = false;
		} else {
			Office office = user.getOffice();
			if(office==null || (office != null && 
					office.getParent() !=null &&
					!ValidateTool.isEmptyOrNull(office.getParent().getId()) &&
					!office.getParent().getId().equals("3"))) {
				addMessage(model, "只有企业用户可以使用该功能");
				pass = false;
			}
		}

		model.addAttribute("pass", pass);
		
	}
	
	@RequestMapping(value = "form",method=RequestMethod.GET)
	public String form(Model model, HttpServletRequest request) {
		TOfficeUp tOfficeUp = null;
		Boolean pass = (Boolean)model.asMap().get("pass");
		if(pass) {
			User user = (User) request.getSession().getAttribute(FSysController.FRONT_USER_IN_SESSION);
			TOfficeUp query = new TOfficeUp();
			query.setCreateBy(user);
			List<TOfficeUp> officeUpList = tOfficeUpService.findList(query);
			if(officeUpList!=null && !officeUpList.isEmpty()) {
				tOfficeUp = officeUpList.get(0);
			}
			if(tOfficeUp == null) {
				tOfficeUp = new TOfficeUp();
				// 设置默认值
				tOfficeUp.setIsdiscount("0");
				tOfficeUp.setIssche("0");
			}
		}
		model.addAttribute("tOfficeUp", tOfficeUp);
		return "modules/officeup/fOfficeUpForm";
	}

	@RequestMapping(value = "form",method=RequestMethod.POST)
	public String save(TOfficeUp tOfficeUp, Model model, HttpServletRequest request) {
		Boolean pass = (Boolean)model.asMap().get("pass");
		if(pass) {
			
			// 校验
			
			String contacts = tOfficeUp.getContacts();
			String contactstel = tOfficeUp.getContactstel();
			String userate = tOfficeUp.getUserate();
			String usetype = tOfficeUp.getUsetype();
			List<String> messages = Lists.newArrayList();
			if(ValidateTool.isEmptyOrNull(contacts)) {
				messages.add("请填写财务联系人");
			}
			if(!ValidateTool.isMobile(contactstel)) {
				messages.add("请填写财务联系人手机");
			}
			if(ValidateTool.isEmptyOrNull(userate)) {
				messages.add("请勾选用车频率");
			}
			if(ValidateTool.isEmptyOrNull(usetype)) {
				messages.add("请勾选用车类型");
			}
			
			if (messages.isEmpty() && beanValidator(model, tOfficeUp)){
				User user = (User) request.getSession().getAttribute(FSysController.FRONT_USER_IN_SESSION);
				tOfficeUp.setUpdateBy(user);
				tOfficeUpService.save(tOfficeUp);
				addMessage(model, "普通客户升级申请成功");
			} else {
				addMessage(model, messages.toArray(new String[]{}));
			}
		}
		model.addAttribute("tOfficeUp", tOfficeUp);
		return "modules/officeup/fOfficeUpForm";
	}
	
	@RequestMapping(value = "delete")
	public String delete(TOfficeUp tOfficeUp, Model model, RedirectAttributes redirectAttributes) {
		Boolean pass = (Boolean)model.asMap().get("pass");
		if(pass) {
			tOfficeUpService.delete(tOfficeUp);
			addMessage(redirectAttributes, "取消升级申请成功");
		}
		return "redirect:/f/officeup/tOfficeUp/form";
	}

}